﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebBanHang.Models;
using WebBanHang.ViewModel;

namespace WebBanHang.Areas.Admin.Controllers
{
    public class NhomSanPhamController : Controller
    {
        private HNCSolutionsEntities db = new HNCSolutionsEntities();

        //
        // GET: /Admin/NhomSanPham/

        public ActionResult Index(int currentPage = 1, int pageRows = 10)
        {            
            var listNhomSanPham = db.NhomSanPhams.OrderBy(sp => sp.ThuTu).Skip((currentPage - 1) * pageRows).Take(pageRows).ToList();
            int totalPages = (db.SanPhams.Count() / pageRows) + (db.SanPhams.Count() % pageRows > 0 ? 1 : 0);
            ListNhomSanPhamViewModel viewModel = new ListNhomSanPhamViewModel 
            {
                ListNhomSanPham = listNhomSanPham,
                Paging = new Paging { CurrentPage = currentPage, PageRows = pageRows, TotalPage = totalPages }
            };

            return View(viewModel);
        }

        //
        // GET: /Admin/NhomSanPham/Details/5

        public ActionResult Details(int id = 0)
        {
            NhomSanPham nhomsanpham = db.NhomSanPhams.Find(id);
            if (nhomsanpham == null)
            {
                return HttpNotFound();
            }
            return View(nhomsanpham);
        }

        //
        // GET: /Admin/NhomSanPham/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Admin/NhomSanPham/Create

        [HttpPost]
        public ActionResult Create(NhomSanPham nhomsanpham)
        {
            if (ModelState.IsValid)
            {
                db.NhomSanPhams.Add(nhomsanpham);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(nhomsanpham);
        }

        //
        // GET: /Admin/NhomSanPham/Edit/5

        public ActionResult Edit(int id = 0)
        {
            NhomSanPham nhomsanpham = db.NhomSanPhams.Find(id);
            if (nhomsanpham == null)
            {
                return HttpNotFound();
            }

            return View(nhomsanpham);
        }

        //
        // POST: /Admin/NhomSanPham/Edit/5

        [HttpPost]
        public ActionResult Edit(NhomSanPham nhomsanpham)
        {
            if (ModelState.IsValid)
            {
                db.Entry(nhomsanpham).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(nhomsanpham);
        }

        public ActionResult Submit(string submitForm, IEnumerable<int> intDeletedIds)
        {
            var nhomSanPhams = this.db.NhomSanPhams.Where(nsp => intDeletedIds.Contains(nsp.NhomSanPhamId)).ToList();
            nhomSanPhams.ForEach(nsp => this.db.NhomSanPhams.Remove(nsp));
            this.db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}